using IQIGame.Onigao.Game;
using IQIGame.Onigao.GamePlay;
using System.Collections.Generic;

namespace IQIGame.Onigao.Logic
{
    /// <summary>
    /// 模拟触发
    /// 不支持的触发器：2
    /// 需要带参数的触发器：6，8，10
    /// 注意该执行在加了新的触发器时，如果没有随即维护，则新触发器不支持模拟，发现报错请找程序
    /// 
    /// 参数组1:1.指定实体的tag或mcid，2.触发类型，3.触发参数1（可选），4.触发参数2（可选）
    /// 
    /// type == 6：参数3：state
    /// type == 8：参数3：类型（1：电梯，2：房子），参数4：进入/离开（1/0）
    /// type == 10：参数3：进入/离开（1/0）
    /// </summary>
    public class ExecSimulateTrigger : BaseServiceExecution
    {
        public override void Execute(LevelRegion region, LevelPlayer player)
        {
            var config = _config as ExecParamSimulateTrigger;
            if (!config.tagOrMcid.TryGetValue(triggerEntity, out int tagOrMcid))
            {
                LogicLog.LogError("无法获取到tagOrMcid.");
                return;
            }

            ServiceTriggerType triggerType = config.triggerType;
            BaseServiceTriggerConfig triggerConfig = config.triggerConfig;

            if (tagOrMcid == 0)
            {
                LogicLog.LogError($"执行{(int)ServiceExecutionType.SimulateTrigger}不支持tag == 0。");
                return;
            }

            TrigValueTypeParam parameter = default;
            switch (triggerType)
            {
                case ServiceTriggerType.Interact:
                case ServiceTriggerType.UnderAttack:
                case ServiceTriggerType.EntityStopNavigation:
                case ServiceTriggerType.NoServiceTiggeredWhenInteract:
                case ServiceTriggerType.EntityBorn:
                case ServiceTriggerType.TeleportArrive:
                    break;
                case ServiceTriggerType.EntityState:
                case ServiceTriggerType.EnterOrLeaveArea:
                    parameter.param1 = (triggerConfig as TrigParamEnterOrLeaveArea).isRequireEnter ? 1 : 0;
                    break;
                case ServiceTriggerType.SwitchPhysicalArea:
                    var switchPhysicalArea = triggerConfig as TrigParamSwitchPhysicalArea;
                    parameter.param1 = switchPhysicalArea.isOut ? 1 : 0;
                    break;
                //case ServiceTriggerType.MoveInRange: //不支持，通过default打印报错，这里明确一下不支持的类型
                //其他新增类型如果没有即时支持，也通过default打印报错。
                default:
                    LogicLog.LogError($"执行{(int)ServiceExecutionType.SimulateTrigger}不支持触发类型 {triggerType}。");
                    return;
            }

            List<LevelEntity> entities = ListPool<LevelEntity>.Get();
            {
                region.GetEntitiesByTagOrMcid(tagOrMcid, entities);
                foreach (LevelEntity entity in entities)
                {
                    if (triggerType == ServiceTriggerType.Interact)
                    {
                        LevelServiceModule.TriggerInteractiveEntity(player, entity, ServiceTriggerType.Interact);
                    }
                    else
                    {
                        entity.TryTriggerService(player, triggerType, null, parameter);
                    }
                }
            }
        }
    }
}
